// pages/ConfirmOrderPage/ConfirmOrderPage.js
var clickThrottle = require("../../utils/clickThrottle.js");
var api = require("../../utils/request.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    proData: {},
    tuangouID: '',
    code: '',
    pickupArr: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this
    that.data.proData = JSON.parse(options.data);
    console.log('cccc' + options.tuangouId)
    that.data.tuangouID = options.tuangouId
    that.data.code = options.code
    var totalPrice = 0;
    var containNoPricePro = false;
    for (let i = 0; i < that.data.proData.length; ++i) {
      var pro = that.data.proData[i];
      totalPrice += pro.userOrderNum * pro.price;
      if (pro.type != 2) {
        containNoPricePro = true
      }
    }

    that.setData({
      isIphoneX: getApp().globalData.statusBarHeight >= 44 ? true : false,
      btmHeight: 120,
      targetData: that.data.proData,
      totalPrice: totalPrice,
      containNoPricePro: containNoPricePro,
      phoneNumber: wx.getStorageSync('phone') == undefined ? '' : wx.getStorageSync('phone'),
      hideModal: true
    })

    that.getSelfPickupList()
  },
  showPickup: function () {
    var that = this

    if (that.data.pickupArr.length == 0) {
      wx.showModal({
        title: '团长暂未添加自提点',
        content: '',
        showCancel: false,
        confirmText: '我知道了',
        complete: (res) => {
          if (res.confirm) {}
        }
      })
      return
    }

    that.setData({
      hideModal: false
    })
  },
  hidePickup: function () {
    var that = this
    that.setData({
      hideModal: true
    })
  },
  selectPickup: function (e) {
    var that = this
    var idx = e.currentTarget.dataset.index;
    var pick = that.data.pickupArr[idx];

    for (let i = 0; i < that.data.pickupArr.length; i++) {
      var picker = that.data.pickupArr[i];
      picker.lastPick = 0
    }
    pick.lastPick = 1;
    that.setData({
      name: pick.name,
      hideModal: true,
      selfPickupList: that.data.pickupArr
    })
  },

  getSelfPickupList: function () {
    var that = this
    api.request1({
      "tuangouId": that.data.tuangouID,
      "path": "user/selfPickupList",
      "token": wx.getStorageSync('token')
    }, function (result) {
      if (result.code == '0') {
        var name = '默认'
        that.data.pickupArr = result.data.selfPickupList;
        for (let i = 0; i < that.data.pickupArr.length; i++) {
          var picker = that.data.pickupArr[i];
          if (picker.lastPick == 1) {
            name = picker.name
          }
        }
        that.setData({
          selfPickupList: result.data.selfPickupList,
          name: name,

        })
      } else {
        wx.showModal({
          title: '提示',
          content: result.msg,
          showCancel: false,
          confirmText: '我知道了',
          complete: (res) => {

            if (res.confirm) {

            }
          }
        })
      }
    }, function (error) {})
  },

  submitOrder: function () {
    var that = this
    var submitProducts = [];

    var pickerID = ''
    for (let i = 0; i < that.data.pickupArr.length; i++) {
      var picker = that.data.pickupArr[i];
      if (picker.lastPick == 1) {
        pickerID = picker.id
      }
    }

    if (pickerID.length == 0) {
      wx.showModal({
        title: '请选择自提点',
        content: '',
        showCancel: false,
        confirmText: '好的',
        complete: (res) => {
          if (res.cancel) {

          }

          if (res.confirm) {

          }
        }
      })
      return
    }

    for (let i = 0; i < that.data.proData.length; ++i) {

      var productModel = {};
      var product = that.data.proData[i];
      productModel.specsId = product.specsId;
      productModel.num = product.userOrderNum;
      submitProducts.push(productModel);
    }
    //console.log("submitProducts==",submitProducts);
    var item = JSON.stringify(submitProducts)

    api.request1({
      "specsList": item,
      "path": "user/addUserSpecs",
      "tuangouId": that.data.tuangouID,
      "pickUpId": pickerID,
      "token": wx.getStorageSync('token')
    }, function (result) {
      // that.getTargetDetail(that.data.tuangouID);
      if (result.code == '0') {
        wx.reLaunch({
          url: '/pages/PayOrderFinishPage/PayOrderFinishPage?tuangouId=' + that.data.tuangouID + '&code=' + that.data.code,
        })
      } else if (result.code == '301') {
        wx.showModal({
          title: '提示',
          content: result.msg,
          showCancel: false,
          confirmText: '我知道了',
          complete: (res) => {

            if (res.confirm) {

            }
          }
        })
      }
    }, function (error) {})
  },
  confirmOrder: function () {
    var that = this
    if (!clickThrottle.clickThrottle()) {
      return
    }
    console.log('ddddd')
    that.submitOrder()
  },
  getrealtimephonenumber(e) {
    var that = this
    console.log(e.detail.code) // 动态令牌
    console.log(e.detail.errMsg) // 回调信息（成功失败都会返回）
    console.log(e.detail.errno) // 错误码（失败时返回）
    if (e.detail.errno == 103) {
      that.submitOrder()
    } else {
      if (e.detail.code != undefined) {
        api.request1({
          "code": e.detail.code,
          "path": "user/getUserPhone",
          "token": wx.getStorageSync('token')
        }, function (result) {
          if (result.code == '0') {
            wx.setStorageSync('phone', result.msg)
            that.setData({
              phoneNumber: wx.getStorageSync('phone'),
            })
            that.submitOrder()
          }
        }, function (error) {})
      }
    }
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  handlerGobackClick() {
    wx.navigateBack({
      delta: 1
    });
  }
})